package com.chromatic.competition.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chromatic.competition.R;
import com.chromatic.competition.model.UserPost;
import com.chromatic.competition.view.notifications.ChatActivity;
import com.chromatic.competition.view.recruitment.TeammateDetailActivity;

import java.util.List;

public class PostListAdapter extends RecyclerView.Adapter<PostListAdapter.ViewHolder> {
    private List<UserPost> userPostList;

    public PostListAdapter(List<UserPost> userPostList) {
        this.userPostList = userPostList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建ViewHolder并关联对应的布局文件
        // 使用LayoutInflater从post_item.xml布局文件创建一个View对象
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 绑定数据到ViewHolder中的视图元素
        // 获取在特定位置的UserPost对象
        UserPost userPost = userPostList.get(position);
        // 调用ViewHolder的bind方法将数据绑定到视图上
        holder.bind(userPost);
//        Log.d("PostListAdapter的onBindViewHolder方法", "已经正确地将数据源中的数据绑定到视图上");

        holder.itemView.setOnClickListener(v -> {
            // 创建一个Intent对象，用于启动ChatActivity
            Intent intent = new Intent(v.getContext(), TeammateDetailActivity.class);
            // 将friend_id作为额外数据传递给ChatActivity
            intent.putExtra("teammate_id", userPost.getUserId());
            // 启动ChatActivity
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        // 返回数据源列表的大小
//        Log.d("PostListAdapter的getItemCount方法", "正确返回了数据源列表的大小：" + userPostList.size());
        return userPostList.size();
    }

    public void setPosts(List<UserPost> userPostList) {
        // 设置数据源列表
        this.userPostList = userPostList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView userIdTextView;
        private final TextView categoryTextView;
        private final TextView competitionNameTextView;
        private final TextView postContentTextView;
        private final TextView postTimeTextView;
        private final TextView startTimeTextView;
        private final TextView endTimeTextView;
        private final TextView expenseTextView;
        private final TextView isExpiredTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // 获取ViewHolder中的视图元素
            // 通过findViewById方法找到post_item.xml布局文件中对应的TextView控件
            userIdTextView = itemView.findViewById(R.id.userIdTextView);
            categoryTextView = itemView.findViewById(R.id.categoryTextView);
            competitionNameTextView = itemView.findViewById(R.id.competitionNameTextView);
            postContentTextView = itemView.findViewById(R.id.postContentTextView);
            postTimeTextView = itemView.findViewById(R.id.postTimeTextView);
            startTimeTextView = itemView.findViewById(R.id.startTimeTextView);
            endTimeTextView = itemView.findViewById(R.id.endTimeTextView);
            expenseTextView = itemView.findViewById(R.id.expenseTextView);
            isExpiredTextView = itemView.findViewById(R.id.isExpiredTextView);
        }

        @SuppressLint("SetTextI18n")
        public void bind(UserPost userPost) {
            // 将数据绑定到视图元素
            userIdTextView.setText("用户ID：" + userPost.getUserId());
            categoryTextView.setText(userPost.getCategory());
            competitionNameTextView.setText(userPost.getCompetitionName());
            postContentTextView.setText(userPost.getPostContent());
            postTimeTextView.setText("发布时间：" + userPost.getPostTime());
            startTimeTextView.setText("开始时间：" + userPost.getStartTime());
            endTimeTextView.setText("截止时间：" + userPost.getEndTime());
            // 设置费用文本
            String expenseText = "悬赏：￥" + userPost.getExpense();
            expenseTextView.setText(expenseText);

            // 设置是否过期文本
            if (userPost.isExpired()) {
                isExpiredTextView.setText("已过期");
            } else {
                isExpiredTextView.setText("进行中");
            }
        }
    }
}